<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="org.apak.model.*" import="org.apak.control.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>ServicePart</title>
</head>
<body>
<%
	ServicePart servicePart = (ServicePart) request.getAttribute("servicePart");
	Device[] deviceArray = (Device[]) request.getAttribute("deviceArray");
	ErrorList errors = (ErrorList) request.getAttribute("errors");
	String success = (String) request.getAttribute("success");
	String serviceOrderID = (String) request.getAttribute("serviceOrderID");
	ServicePartForm servicePartForm = (ServicePartForm) request.getAttribute("servicePartForm");
	String[] fields = {"", "", "", "", "0"};
	String[] errorsText = {"", "", "", ""};
	if(errors != null) {
		servicePartForm = (ServicePartForm) errors.getForm();
		fields[0] = servicePartForm.getPart_name();
		fields[1] = servicePartForm.getSerial_no();
		fields[2] = servicePartForm.getPart_count();
		fields[3] = servicePartForm.getPart_price();
		fields[4] = servicePartForm.getService_device_fk();
		errorsText[0] = errors.getError("part_name");
		errorsText[2] = errors.getError("part_count");
		errorsText[3] = errors.getError("part_price");
	} else if(servicePartForm != null) {
		fields[0] = servicePartForm.getPart_name();
		fields[1] = servicePartForm.getSerial_no();
		fields[2] = servicePartForm.getPart_count();
		fields[3] = servicePartForm.getPart_price();
		fields[4] = servicePartForm.getService_device_fk();
	}  else if(servicePart != null) {
		fields[0] = servicePart.getPart_name();
		fields[1] = servicePart.getSerial_no();
		fields[2] = Integer.toString(servicePart.getPart_count());
		fields[3] = Integer.toString(servicePart.getPart_price());
		fields[4] = Integer.toString(servicePart.getService_device_fk());
	}
	if(success == null)
		success = "";
%>
<%
	if(serviceOrderID != null) {
%>
<a href="./serviceOrder?id=<%= serviceOrderID %>">... back to service order</a>
<%
	}
%>
<form action='servicePart?action=save' method='POST'>
	<table>
		<tr>
			<td>Part name</td>
			<td><input type="text" name="part_name" value="<%= fields[0] %>"><%= errorsText[0] %></td>
		</tr>
		<tr>
			<td>Serial No</td>
			<td><input type="text" name="serial_no" value="<%= fields[1] %>"><%= errorsText[1] %></td>
		</tr>
		<tr>
			<td>Device</td>
			<td>
				<select name="device">
				<%
					if(fields[4].equals("0")) {
				%>
				<option value="0" selected>Select device</option>
				<%
					} else {
				%>
				<option value="0">Select device</option>
<%				
					}
				if(deviceArray != null)
				for(int k = 0; k < deviceArray.length; k++) {
					Device device = deviceArray[k];
					if(fields[4].equals(Integer.toString(device.getDevice()))) {
%>
						<option value="<%= device.getDevice() %>" selected><%= device.getName() %>, <%= device.getModel() %></option>
					<%
					} else {
					%>
						<option value="<%= device.getDevice() %>"><%= device.getName() %>, <%= device.getModel() %></option>
<%	
					}
				}
%>
			</select>
		</td>
		</tr>
		<tr>
			<td>Part count</td>
			<td><input type="text" name="part_count" value="<%= fields[2] %>"><%= errorsText[2] %></td>
		</tr>
		<tr>
			<td>Price</td>
			<td><input type="text" name="part_price" value="<%= fields[3] %>"><%= errorsText[3] %></td>
		</tr>
	</table>
	<input type="submit" name="actionButton" value="Save"/> <%= success %>
</form>
</body>
</html>